package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.UserMapper;
import cn.tedu._04weibo.mapper.WeiboMapper;
import cn.tedu._04weibo.pojo.dto.WeiboAddParam;
import cn.tedu._04weibo.pojo.entity.Weibo;
import cn.tedu._04weibo.pojo.vo.UserLoginVo;
import cn.tedu._04weibo.pojo.vo.WeiboDetailVo;
import cn.tedu._04weibo.pojo.vo.WeiboIndexVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Tag(name = "_02微博模块")
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @Autowired
    private UserMapper userMapper;

    @PostMapping("/insert")
    @Operation(summary = "添加微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insertWeibo(@RequestBody WeiboAddParam param, HttpSession session) {
        log.info("微博内容:{}", param.getContent());

        UserLoginVo userLoginVo = (UserLoginVo) session.getAttribute("user");

        log.debug("userLoginVo:{}", userLoginVo);

        if (userLoginVo != null) {
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(param, weibo);
            weibo.setUserId(userLoginVo.getId());
            weibo.setCreateTime(new Date());


            int i = weiboMapper.insertWeibo(weibo);
            if (i > 0) {
                return JsonResult.ok();
            }
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return new JsonResult(StatusCode.NOT_LOGIN);

    }

    @GetMapping("/selectIndex")
    @Operation(summary = "查询微博首页")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex(HttpSession session) {
        log.info("查询微博首页");

        UserLoginVo userLoginVo = (UserLoginVo) session.getAttribute("user");

        if(userLoginVo != null) {
            List<WeiboIndexVo> weiboList = weiboMapper.selectIndex(userLoginVo.getId());

            for (WeiboIndexVo weibo : weiboList) {
                weibo.setNickname(userMapper.selectNickname(userLoginVo.getId()));
            }

            return JsonResult.ok(weiboList);
        }
        return new JsonResult(StatusCode.NOT_LOGIN);
    }

    @GetMapping("/selectById")
    @Operation(summary = "根据id查询微博详情")
    @ApiOperationSupport(order = 30)
    public JsonResult selectById(@RequestParam @Range(min = 200,
            max = 500,message = "要求微博的编号id必须在200-500之间") Integer id) {
        log.info("根据id查询微博详情:{}", id);
        WeiboDetailVo weiboDetailVo = weiboMapper.selectById(id);
        if (weiboDetailVo != null) {
            return JsonResult.ok(weiboDetailVo);
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

}
